package com.aptemo.webetek.client.widgets.stakes;

import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.GetBetsPagingAction;
import com.aptemo.webetek.client.enums.EnumBetStatus;
import com.aptemo.webetek.client.events.StakeSelectedEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.LoginProposalWidget;
import com.aptemo.webetek.client.widgets.paging.PagingPresenter;
import com.aptemo.webetek.shared.BetBean;
import com.aptemo.webetek.shared.PagingResult;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class StakesPagingPresenter
		extends PagingPresenter implements ClickHandler
{

	private HandlerManager			meventbus;
	private WebetekUserServiceAsync	mservice;
	protected boolean				misAdmin	= false;
	private StakesDisplay			display;
	protected List<BetBean>			list;
	private String					teamId;

	private HasWidgets				container;

	private int						pageNo		= 0;

	public StakesPagingPresenter(StakesDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String teamId)
	{
		this(view, eventbus, userService, teamId, "", 0);
	}

	public StakesPagingPresenter(StakesDisplay view, HandlerManager eventbus, WebetekUserServiceAsync userService, String teamId, String filter, int pageNo)
	{
		super(view.getPagingView());

		this.meventbus = eventbus;
		this.mservice = userService;
		this.display = view;
		
		init(null, teamId, filter, pageNo);
		
	}

	private void bind()
	{
		display.setTarget(teamId);

		display.setStatusValue(EnumBetStatus.OPENED);

		display.getStatusFilter().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				resetPaging();
				retrieveList(0);
			}

		});

		display.getList().addClickHandler(this);
	}

	private void retrieveList(final int page)
	{
		final String destPage = getCursor(page);

		display.getInfoField().setText(display.translate("loading"));

		GetBetsPagingAction action = new GetBetsPagingAction(teamId, display.getStatusFilterValue(), destPage, getPageSize());
		mservice.invoke(action, new GotAsynchCallback<PagingResult<BetBean>>()
		{

			@Override
			public void onSuccess(PagingResult<BetBean> result)
			{
				// paging staff...
				if (result.getTotalCnt() >= 0)
				{
					setPagingTotalSize(result.getTotalCnt());
				}
				pageNo = updatePaging(page, result.getCursor());

				StakesPagingPresenter.this.list = result.getList();
				display.getInfoField().setText("");
				display.updateStakeList(list);

			}

			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.updateStakeList(null);
				if (code == EErrorCodes.LOGIN_ERROR)
				{
					container.clear();
					display.getInfoField().setText("");
					container.add(new LoginProposalWidget());
				}
				else
				{
					display.getInfoField().setText(message);
				}
			}

		});
	}

	@Override
	public void go(HasWidgets container)
	{
		this.container = container;
		container.clear();
		container.add(display.getDisplay());
		bind();

		retrieveList(pageNo);
	}

	@Override
	public void onClick(ClickEvent event)
	{
		if (event.getSource() == display.getList())
		{
			int row = display.getClickedRow(event);
			// StakeBean stake = getItem(row);
			BetBean stake = list.get(row);
			// update bet slip
			meventbus.fireEvent(new StakeSelectedEvent(stake.getId(), teamId));
		}

	}

	private void init(UserBean currentUser, String teamid, String filter, int pageNo)
	{
		this.teamId = teamid;
		this.pageNo = pageNo;

//		if (filter != null && !filter.isEmpty())
//		{
//			EnumBetStatus status;
//			try
//			{
//				status = EnumBetStatus.valueOf(filter);
//				display.setStatusValue(status);
//			}
//			catch (Exception e)
//			{
//				status = EnumBetStatus.OPENED;
//			}
//		}

	}

	public void update(UserBean currentUser, String teamId, String filter, int pageNo)
	{
		init(currentUser, teamId, filter, pageNo);

		retrieveList(pageNo);
	}


	// public void update(int pageNo)
	// {
	// retrieveList(0);
	// }

}
